<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>添加品牌</title>
    <script src="js/axios.min.js"></script>
</head>
<body onload="getParams()">
<h3>添加品牌</h3>
<form action="#" method="post">
    <input type="hidden" id="id" name="id">
    品牌名称：<input id="brandName" name="brandName"><br>
    企业名称：<input id="companyName" name="companyName"><br>
    排序：<input id="ordered" name="ordered"><br>
    描述信息：<textarea rows="5" cols="20" id="description" name="description"></textarea><br>
    状态：
    <input type="radio" name="status" value="0">禁用
    <input type="radio" name="status" value="1">启用<br>

    <input type="button" id="btn"  value="提交" onclick="requestAddOrUpdateBrand()">
</form>
<script type="text/javascript">

    function requestAddOrUpdateBrand()
    {
        //1.获取表单数据
        var brandName = document.getElementById("brandName").value;
        var companyName = document.getElementById("companyName").value;
        var ordered = document.getElementById("ordered").value;
        var description = document.getElementById("description").value;
        var status = document.getElementsByName("status");
        var id = document.getElementById("id").value;
        var reqUrl = "/brand-case/brandAdd";
        //2. 封装请求参数
        var params = {
            "id":id,
            "brandName":brandName,
            "companyName" :companyName,
            "ordered": ordered,
            "description" :description,
            "status" : status[0].checked ? 0 : 1
        };


        //隐藏id是否有值
        if(id != "" && id!=null && id != undefined)
        {
            reqUrl = "/brand-case/brandEdit";
        }else {

            reqUrl ='/brand-case/brandAdd';
        }


        //3.发送Ajax请求
        axios({
            url: reqUrl,
            data:params,
            method:'POST'
        }).then((response)=>{

            //成功跳转列表页
            if(response.data )
            {
                window.location.href="brand.html"
            }else
            {
                alert("添加失败");
            }

        })



    }

    /**
     * 获取主键
     */

    function getParams()
    {
       var queryString = window.location.search;
       if(queryString.length > 0)
       {
            let urlParams = new URLSearchParams(queryString);
            let id= urlParams.get("id")
           axios({
               url:'/brand-case/brandSel?id='+id,
               method:'GET'
           }).then((response)=>{
               let brand = response.data;
               document.getElementById("id").value = brand.id;
               document.getElementById("brandName").value = brand.brandName
               document.getElementById("companyName").value = brand.companyName
               document.getElementById("ordered").value = brand.ordered;
               document.getElementById("description").value = brand.description;
               var status =document.getElementsByName("status");
               if(brand.status=="0")
               {
                   status[0].checked = true
               }else
               {
                   status[1].checked = true
               }

           })

       }
    }

</script>

</body>
</html>